<?php


namespace app\admin\controller;


use app\admin\base\JwBase;
use think\Exception;
use think\facade\Validate;

class Role extends JwBase
{
    /**
     * info: 角色列表
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 16:13
     * Url: roles
     */
    public function roles()
    {
        $data = \app\model\Role::getRoleAll();
        foreach ($data as $key => $val) {
            $data[$key]['children'] = \app\model\Menu::getUserMenus($val['role']);
            if(empty($data[$key]["children"])){
                unset($data[$key]["children"]);
            }
        }
        $this->respInfo(1000, $data);
    }

    /**
     * info: 添加角色
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 16:13
     * Url: addrile
     */
    public function addrole()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'name|权限名称' => ['require', 'max' => 25, 'min' => 2],
                    'content|说明' => ['require', 'max' => 50, 'min' => 1]
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $dataRole["name"] = $postData['name'];
                $dataRole["content"] = $postData['content'];


                $role = new \app\model\Role();
                $role->save($dataRole);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 更新角色
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 16:20
     * Url: uprole
     */
    public function uprole()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'rid|操作ID' => ['require', 'number', 'max' => 10, 'min' => 1],
                    'name|权限名称' => ['require', 'max' => 25, 'min' => 2],
                    'content|说明' => ['require', 'max' => 50, 'min' => 1]
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $dataRole["name"] = $postData['name'];
                $dataRole["content"] = $postData['content'];

                $role = \app\model\Role::find($postData['rid']);
                $role->save($dataRole);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 删除角色
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 16:54
     * Url: delRole
     */
    public function delRole(){
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                if (isset($postData['rid']) && !empty($postData['rid'])) {
                    $data = \app\model\Role::find($postData['rid']);
                    $data->delete();
                    $this->respInfo(1000);
                } else {
                    $this->respInfo(2500, [], '参数错误');
                }
            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 修改权限
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 16:55
     * Url: editRole
     */
    public function editRole(){
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'rid|操作ID' => ['require', 'number', 'max' => 10, 'min' => 1],
                    'role|权限' => ['require'],
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $dataRole["role"] = $postData['role'];

                $role = \app\model\Role::find($postData['rid']);
                $role->save($dataRole);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }
        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 修改权限节点
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 16:55
     * Url: delNode
     */
    public function delNode(){
        return "1";
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'rid|操作ID' => ['require', 'number', 'max' => 10, 'min' => 1],
                    'mid|权限' => 'require',
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                // 查当前角色 所有权限
                $role = \app\model\Role::find($postData['rid']);
                $roleStr = $role->role;
                $roleArr = explode(',',$roleStr);

                // 查当前节点 下权限
                $menu = \app\model\Menu::getSon($postData['mid']);
                array_push($menu,$postData['mid']);

                // 取出差集 保存
                $newRole = array_diff($roleArr,$menu);
                $role->role = implode(',',$newRole);
                $role->save();
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }
        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }
}